package config

import (
	"github.com/chicken-team-outside/chicken_transmission/client_core/service"
	"github.com/chicken-team-outside/chicken_transmission/model"
	"sync/atomic"
)

var localDriverInfo atomic.Value
var localDriverEnabled atomic.Bool

func initLocalDiskDriver() error {
	dr := &DriverInfo{
		UserName:       "本地磁盘",
		Vendor:         model.Vendor_Local,
		ConnectionInfo: []byte(`""`),
	}
	err := service.GetOrSaveConfig([]byte("local_driver_info"), dr)
	if err != nil {
		return err
	}
	localDriverInfo.Store(dr)

	enabled := false
	err = service.GetOrSaveConfig([]byte("local_driver_enabled"), &enabled)
	if err != nil {
		return err
	}
	localDriverEnabled.Store(enabled)
	return nil
}

func LocalDiskDriverInfo() *DriverInfo {
	return localDriverInfo.Load().(*DriverInfo)
}

func SaveLocalDiskDriver(info *DriverInfo) error {
	err := service.SaveConfigData([]byte("local_driver_info"), info)
	if err == nil {
		localDriverInfo.Store(info)
	}
	return err
}

func LocalDiskEnabled() bool {
	return localDriverEnabled.Load()
}

func SaveLocalDiskEnabled(enabled bool) error {
	err := service.SaveConfigData([]byte("local_driver_enabled"), enabled)
	if err == nil {
		localDriverEnabled.Store(enabled)
	}
	return err
}
